<template>
  <div class="app-container">
    <!-- 查询条件开始 -->
    <!-- 查询条件开始 -->
    <el-form ref="queryForm" :model="queryParams" :inline="true" label-width="68px">
      <el-form-item label="字典类型" prop="dictType">
        <!--  prop="dictType --- 与v-model="queryParams.dictType" 对应 才能默认选中  -->
        <el-select v-model="queryParams.dictType" placeholder="请选择字典类型" clearable size="small" style="width:240px" @change="selectDictTypeOptions">
          <el-option v-for="dict in typeOptions" :key="dict.dictType" :label="dict.dictName" :value="dict.dictType" />
        </el-select>
      </el-form-item>
      <el-form-item label="字典标签" prop="dictLabel">
        <el-input v-model="queryParams.dictLabel" placeholder="请输入字典标签" clearable size="small" style="width:240px" />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="字典状态" clearable size="small" style="width:240px">
          <el-option v-for="dict in statusOptions" :key="dict.dictValue" :label="dict.dictLabel" :value="dict.dictValue" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button type="primary" icon="el-icon-refresh" size="mini" @click="restQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <!-- 查询条件结束 -->

    <!-- 表头按钮开始 -->
    <el-row :gutter="10" style="margin-bottom: 8px;">
      <el-col :span="1.5">
        <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete">删除</el-button>
        <el-button type="warning" icon="el-icon-refresh-left" size="mini" :disabled="multiple">
          <router-link :to="'/system/dict'" class="link-type">
            <span style="color: #fff;">返回字典类型</span>
          </router-link>
        </el-button>
      </el-col>

    </el-row>
    <!-- 表头按钮结束 -->

    <!-- 数据表格开始 -->
    <el-table v-loading="loading" border :data="dictDataTableList" @selection-change="handleSelectionChnage">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="字典编码" prop="dictCode" align="center" />
      <el-table-column label="字典标签" prop="dictLabel" align="center" />
      <el-table-column label="字典键值" prop="dictValue" align="center" />
      <el-table-column label="字典排序" prop="dictSort" align="center" />
      <el-table-column label="状态" prop="status" align="center" :formatter="statusFormatter" />
      <el-table-column label="备注" prop="remark" align="center" :show-overflow-tooltip="true" />
      <el-table-column label="创建时间" prop="createTime" align="center" width="180" />
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button type="text" icon="el-icon-edit" size="mini" @click="handleUpdate(scope.row)">修改</el-button>
          <el-button type="text" icon="el-icon-delete" size="mini" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 数据表格结束 -->

    <!-- 分页插件开始 -->
    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getDictDataList" />
    <!-- 分页插件开始 -->

    <!-- 添加or修改组件开始 -->
    <el-dialog
      :title="title"
      :visible.sync="open"
      width="500px"
      center
      append-to-body
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="字典类型" prop="dictType">
          <el-input v-model="form.dictType" :disabled="true" size="small" />
        </el-form-item>
        <el-form-item label="数据标签" prop="dictLabel">
          <el-input v-model="form.dictLabel" placeholder="请输入数据标签" clearable size="small" />
        </el-form-item>
        <el-form-item label="数据键值" prop="dictValue">
          <el-input v-model="form.dictValue" placeholder="请输入数据键值" clearable size="small" />
        </el-form-item>
        <el-form-item label="排序显示" prop="dictSort">
          <el-input-number v-model="form.dictSort" placeholder="请输入数据键值" clearable size="small" :min="0" />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio
              v-for="dict in statusOptions"
              :key="dict.dictValue"
              :label="dict.dictValue"
              :value="dict.dictValue"
            >{{ dict.dictLabel }}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入字典备注" clearable size="small" />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="handleSubmit">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </span>
    </el-dialog>
    <!-- 添加or修改组件结束 -->
  </div>
</template>
<!--
addDictData, updateDictData, deleteDictDataByIds, getDictDataById, getDataByType
addDictData, updateDictData, deleteDictDataByIds, getDictDataById, getDataByType
-->
<script>
import { listForPage } from '@/api/system/dict/data'
import Pagination from '@/components/Pagination'
import { getDictTypeById, selectAllDictType } from '@/api/system/dict/type'
import { addDictData, updateDictData, deleteDictDataByIds, getDictDataById } from '../../../api/system/dict/data'
export default {
  components: {
    Pagination
  },
  data() {
    return {
      // 遮罩层
      loading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 字典数据表格数据
      dictDataTableList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 状态数据字典
      statusOptions: [],
      // 类型数据字典
      typeOptions: [],
      // 默认字典类型
      defaultDictType: undefined,
      // 新增字典类型
      insertDictType: undefined,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        dictLabel: undefined, // 标识
        dictType: undefined, // 字典类型
        status: undefined // 状态
      },
      // 表单数据
      form: {},
      // 表单校验
      rules: {
        dictLabel: [
          { required: true, message: '数据标签不能为空', trigger: 'blur' }
        ],
        dictValue: [
          { required: true, message: '数据键值不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    // 获取根据类型获取字典数据的 数据
    this.getDataByType('sys_normal_disable').then(res => {
      this.statusOptions = res.data
    })
    // 取路由路径上的参数
    const dicttype = this.getRouteParams()
    // 查询条件的下拉框 给到默认值
    getDictTypeById(dicttype.dictId).then(res => {
      console.log(res.data)
      if (res.data.status === '1') {
        this.queryParams.dictType = ''
        return
      }
      this.queryParams.dictType = res.data.dictType
      this.defaultDictType = res.data.dictType
    })
    // 查询所有的字典类型
    selectAllDictType().then(res => {
      this.typeOptions = res.data
      setTimeout(() => {
        // 最后进行加载数据
        this.getDictDataList()
      }, 10)
    })
  },
  methods: {
    // 根据类型查询 字典数据
    getDictDataList() {
      this.loading = true
      console.log(this.queryParams)
      listForPage(this.queryParams).then(res => {
        this.dictDataTableList = res.data // 注入字典数据表格
        this.total = res.total // 注入总记录数
        this.loading = false// 关闭遮罩
      })
    },
    // 监听下拉框的点击
    selectDictTypeOptions(val) {
      this.queryParams.dictType = val
      this.getDictDataList()
      // 修改新增标识
      this.insertDictType = val
    },
    // 翻译状态
    statusFormatter(row) {
      return this.selectDictLabel(this.statusOptions, row.status)
    },
    // 条件查询
    handleQuery() {
      this.getDictDataList()
    },
    // 重置查询条件
    restQuery() {
      this.dateRange = []
      this.resetForm('queryForm')
      this.queryParams.dictType = this.defaultDictType
      this.getDictDataList()
    },
    // 数据表格的多选择框选择时触发
    handleSelectionChnage(selection) {
      // 记录选中的ID/删除
      this.ids = selection.map(item => item.dictCode)
      // 修改 如果选中数大于1
      this.single = selection.length !== 1
      // 删除 如果选中的数不为空 false/true
      this.multiple = !selection.length
    },
    // 重置表单
    reset() {
      this.form = {
        dictCode: undefined,
        dictLable: undefined,
        dictValue: undefined,
        dictType: undefined,
        status: '0',
        dictSort: 0,
        remark: undefined
      }
      // this.queryParams.dictType = this.defaultDictType
      this.resetForm('form')
    },
    // 打开添加的弹出层
    handleAdd() {
      this.open = true
      this.title = '添加字典类型'
      this.reset()
      this.form.dictType = this.insertDictType
    },
    // 打开修改的弹出层
    handleUpdate(row) {
      // 判断如果row为空那么就使用选中的
      console.log(row.dictCode + '当前的修改id')
      this.open = true
      this.title = '修改字典类型'
      this.reset()
      // 根据字典数据id查询字典数据
      getDictDataById(row.dictCode || this.ids).then(res => {
        this.form = res.data
      })
    },
    // 保存方法
    handleSubmit() {
      this.loading = true
      this.$refs['form'].validate((valid) => {
        if (valid) {
          if (this.form.dictCode === undefined) {
            addDictData(this.form).then(res => {
              // 提示
              if (res.code === 200) this.msgSuccess(res.msg)
              else this.msgError(res.msg)
              // 刷新一下下
              this.queryParams.dictType = this.defaultDictType
              this.getDictDataList()
              // 关闭加载遮罩
              this.loading = false
              // 关闭遮罩
              this.open = false
            }).catch(() => {
              this.loading = false
            })
          } else {
            // 修改
            updateDictData(this.form).then(res => {
              // 提示
              if (res.code === 200) this.msgSuccess(res.msg)
              else this.msgError(res.msg)
              // 刷新一下下
              this.queryParams.dictType = this.defaultDictType
              this.getDictDataList()
              // 关闭加载遮罩
              this.loading = false
              // 关闭遮罩
              this.open = false
            }).catch(() => {
              this.loading = false
            })
          }
        } else {
          this.loading = false
          return false
        }
      })
    },
    cancel() {
      this.reset()
      this.open = false
    },
    // 删除
    handleDelete(row) {
      const dictCodes = row.dictCode || this.ids
      this.$confirm('此操作将永久删除该字典数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.loading = true
        deleteDictDataByIds(dictCodes).then(res => {
          this.loading = false
          this.msgSuccess('删除成功')
          this.reset()
          this.getDictDataList()// 全查询
        })
      }).catch(() => {
        this.msgError('删除已取消')
        this.loading = false
      })
    }
  }
}
</script>

<style>

</style>
